package com.apestech.framework.sql;

import com.apestech.framework.json.JSONObject;
import com.apestech.framework.sql.lib.DeleteSql;
import com.apestech.framework.sql.lib.InsertSql;
import com.apestech.framework.sql.lib.SelectSql;
import com.apestech.framework.sql.lib.UpdateSql;
import com.apestech.framework.util.StringUtil;

/**
 * SQL解析器，注意一个对象仅只对一份SQL有效
 * @author hupo
 *
 */
public class SqlPaste extends StringUtil {
	
	private String sqlType;
	private String sql;
	private String tabname;
	private SelectSql select=null;
	private UpdateSql update=null;
	private DeleteSql delete=null;
	private InsertSql insert=null;
	
	public SqlPaste(String sql) throws Exception{
		if(sql==null){
			throw new Exception(getMessage("SqlPaste_sqlIsNull","SQL语句不允许为空"));
		}
		this.sql=sql;
		String operSql=sql.toLowerCase().trim();
		if(operSql.indexOf("update")==0){
			this.sqlType="UPDATE";
			this.update=new UpdateSql(sql);
			this.tabname=this.update.getTableName();
		} else if(operSql.indexOf("insert")==0){
			this.sqlType="INSERT";
			this.insert=new InsertSql(sql);
			this.tabname=this.insert.getTableName();
		} else if(operSql.indexOf("delete")==0){
			this.sqlType="DELETE";
			this.delete=new DeleteSql(sql);
			this.tabname=this.delete.getTableName();
		} else if(operSql.indexOf("select")==0){
			this.sqlType="SELECT";
			this.select=new SelectSql(this.sql);
			this.tabname=this.select.getSelFromTable();
		}
	}
	
	/**
	 * 注意查询类型的SQL不支持显示表名，查询SQL可通过getSelFromTable方法获取
	 * @return
	 */
	public String getTableName(){
		return this.tabname;
	}
	
	/**
	 * 返回空串代表无错，有错返回错误信息
	 * @param alldata 针对UPDATE和DELETE进行全表操作检查
	 * @return
	 */
	public String check(boolean alldata){
		try {
			if("SELECT".equals(this.sqlType)){
				this.select.check(alldata);
			} else if("UPDATE".equals(this.sqlType)){
				this.update.check(alldata);
			} else if("DELETE".equals(this.sqlType)){
				this.delete.check(alldata);
			} else if("INSERT".equals(this.sqlType)){
				this.insert.check();
			}
		} catch (Exception e) {
			return e.getMessage();
		} 
		return "";
	}
	
	/**
	 * 返回空串代表无错，有错返回错误信息，不会判断全表操作
	 * @return
	 */
	public String check(){
		return check(true);
	}
	
	/**
	 * 获取SQL类型
	 * @return
	 */
	public String getSqlType(){
		return this.sqlType;
	}
	
	/**
	 * 获取SQL结构
	 * @return
	 * @throws Exception
	 */
	public JSONObject getSqlStructure() throws Exception{
		if(select!=null){
			return select.getSqlStructure();
		} else if(insert!=null){
			return insert.getSqlStructure();
		} else if(update!=null){
			return update.getSqlStructure();
		} else if(delete!=null){
			return delete.getSqlStructure();
		}
		return null;
	}

	/*public String getSql(String type) throws Exception {
		return getSql(type, null, true);
	}

	public String getSql(String type, String filter, boolean in) throws Exception {
		if(table!=null){
			if("select".equals(type)){
				return table.getSelect(filter, in);
			} else if("insert".equals(type)) {
				return table.getInsert(filter, in);
			} else if("update".equals(type)) {
				return table.getUpdate(filter, in);
			} else if("delete".equals(type)) {
				return table.getDelete();
			}
		}
		return "";
	}*/

}
